const { JxbHttpClient, Session, default: SessionUtil } = require("../../../data/session/session")
const { default: InteractionUtil } = require("../../../shell/ui/interaction")

// subPage/goodsAi/recognize/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cameraShotted: false,
    photoSrc: "", // 拍摄的图片或选则的图片的路径
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      cameraShotted: false,
      photoSrc: "",
    })
    this.cameraCtx = wx.createCameraContext()
  },
  // 拍照
  takePhoto() {
    this.cameraCtx.takePhoto({
      quality: 'high',
      success: (res) => {
        this.setData({
          photoSrc: res.tempImagePath,
          cameraShotted: true
        })
      }
    })
  },
  // 重拍
  rehotograph(){
    this.setData({
      photoSrc: "",
      cameraShotted: false
    })
  },
  cancalHandler(){
    wx.navigateBack()
  },
  // 前往识别
  gotoRecognize(){
    wx.navigateTo({
      url: '/subPage/goodsAi/recogResult/index?pSrc='+encodeURI(this.data.photoSrc),
      fail: err=>{
        InteractionUtil.showNotice("前往识别页面出错！"+err.errMsg)
      }
    })
  },
  chooseImage(){
    wx.chooseImage({
      count: 1,
      success: res=>{
        const {tempFilePaths} = res
        this.setData({
          photoSrc: tempFilePaths[0],
          cameraShotted: true
        })
      }
    })
  },
  // 默认错误处理方法
  error(e) {
    InteractionUtil.showNotice(e.detail)
    console.log(e.detail)
  }
})